# Remove WebExperience

# winget uninstall MicrosoftWindows.Client.WebExperience_cw5n1h2txyewy

# PowerShell >= 6 already has built-in `Remove-Alias`.
if ($PSVersionTable.PSVersion.Major -le 5) {
  function Remove-Alias ([string] $AliasName) {
    while (Test-Path Alias:$AliasName) {
      Remove-Item Alias:$AliasName -Force 2> $null
    }
  }
}

function Format-AliasDefinition {
  param (
    [Parameter(Mandatory = $true)][string] $Definition
  )

  $definitionLines = $Definition.Trim() -split "`n" | ForEach-Object {
    $line = $_.TrimEnd()

    # Trim 1 indent
    if ($_ -match "^`t") {
      return $line.Substring(1)
    } elseif ($_ -match '^    ') {
      return $line.Substring(4)
    }

    return $line
  }

  return $definitionLines -join "`n"
}

<#
.SYNOPSIS
  Get git aliases' definition.
.DESCRIPTION
  Get definition of all git aliases or specific alias.
.EXAMPLE
  PS C:\> Get-Git-Aliases
  Get definition of all git aliases.
.EXAMPLE
  PS C:\> Get-Git-Aliases -Alias gst
  Get definition of `gst` alias.
#>
function Get-Git-Aliases ([string] $Alias) {
  $esc = [char] 27
  $green = 32
  $magenta = 35

  $Alias = $Alias.Trim()
  $blacklist = @(
    'GitCurrentBranch',
    'Remove-Alias',
    'Format-AliasDefinition',
    'Get-Git-Aliases'
  )
  $aliases = Get-Command -Module git-aliases | Where-Object { $_ -notin $blacklist }

  if (-not ([string]::IsNullOrEmpty($Alias))) {
    $foundAliases = $aliases | Where-Object -Property Name -Value $Alias -EQ
    if ($foundAliases -is [array]) {
      return Format-AliasDefinition($foundAliases[0].Definition)
    } else {
      return Format-AliasDefinition($foundAliases.Definition)
    }
  }

  $aliases = $aliases | ForEach-Object {
    $name = $_.Name
    $definition = Format-AliasDefinition($_.Definition)
    $definition = "$definition`n" # Add 1 line break for some row space

    return [PSCustomObject]@{
      Name       = $name
      Definition = $definition
    }
  }

  $cols = @(
    @{
      Name       = 'Name'
      Expression = {
        # Print alias name in green
        "$esc[$($green)m$($_.Name)$esc[0m"
      }
    },
    @{
      Name       = 'Definition'
      Expression = {
        # Print alias definition in yellow
        "$esc[$($magenta)m$($_.Definition)$esc[0m"
      }
    }
  )

  return Format-Table -InputObject $aliases -AutoSize -Wrap -Property $cols
}

<#
.SYNOPSIS
    Prettifies JSON output.
.DESCRIPTION
    Reformats a JSON string so the output looks better than what ConvertTo-Json outputs.
.PARAMETER Json
    Required: [string] The JSON text to prettify.
.PARAMETER Minify
    Optional: Returns the json string compressed.
.PARAMETER Indentation
    Optional: The number of spaces (1..1024) to use for indentation. Defaults to 4.
.PARAMETER AsArray
    Optional: If set, the output will be in the form of a string array, otherwise a single string is output.
.EXAMPLE
    $json | ConvertTo-Json  | Format-Json -Indentation 2
#>
function Format-Json {
  [CmdletBinding(DefaultParameterSetName = 'Prettify')]
  Param(
    [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
    [string]$Json,

    [Parameter(ParameterSetName = 'Minify')]
    [switch]$Minify,

    [Parameter(ParameterSetName = 'Prettify')]
    [ValidateRange(1, 1024)]
    [int]$Indentation = 4,

    [Parameter(ParameterSetName = 'Prettify')]
    [switch]$AsArray
  )

  if ($PSCmdlet.ParameterSetName -eq 'Minify') {
    return ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100 -Compress
  }

  # If the input JSON text has been created with ConvertTo-Json -Compress
  # then we first need to reconvert it without compression
  if ($Json -notmatch '\r?\n') {
    $Json = ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100
  }

  $indent = 0
  $regexUnlessQuoted = '(?=([^"]*"[^"]*")*[^"]*$)'

  $result = $Json -split '\r?\n' |
    ForEach-Object {
      # If the line contains a ] or } character,
      # we need to decrement the indentation level unless it is inside quotes.
      if ($_ -match "[}\]]$regexUnlessQuoted") {
        $indent = [Math]::Max($indent - $Indentation, 0)
      }

      # Replace all colon-space combinations by ": " unless it is inside quotes.
      $line = (' ' * $indent) + ($_.TrimStart() -replace ":\s+$regexUnlessQuoted", ': ')

      # If the line contains a [ or { character,
      # we need to increment the indentation level unless it is inside quotes.
      if ($_ -match "[\{\[]$regexUnlessQuoted") {
        $indent += $Indentation
      }

      $line
    }

  if ($AsArray) {
    return $result
  }

  return $result -Join [Environment]::NewLine
}

function GuessNum {
  $number = Get-Random -Minimum 1 -Maximum 100
  Write-Host "Guess a number between 1 and 100" -ForegroundColor DarkYellow
  $guess = 0
  do {
    $guess = Read-Host -Prompt "What's your guess?`n"
    if ($guess -lt $number) {
      Write-Output 'Too low!'
    } elseif ($guess -gt $number) {
      Write-Output 'Too high!'
    }
  } while ($guess -ne $number)
  Write-Host "Congratulations! You made it!" -ForegroundColor DarkGreen
}